package ru.linnenson.darshana.infrastructure.services.sequrity;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import ru.linnenson.darshana.infrastructure.persistence.models.UserStore;
import ru.linnenson.darshana.infrastructure.persistence.repositories.UserPersistenceRepository;

@Service
@RequiredArgsConstructor
public class LocalUserDetailsService implements UserDetailsService {

  private final UserPersistenceRepository userPersistenceRepository;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    final UserStore user = userPersistenceRepository.findByUsername(username);

    if (user == null) {
      throw new UsernameNotFoundException(username);
    }
    return new LocalUserPrincipal(user);
  }
}
